package cn.wbu.zyh.controller.user;

import cn.wbu.zyh.dto.ArticleDTO;
import cn.wbu.zyh.dto.VersionDTO;
import cn.wbu.zyh.result.Result;
import cn.wbu.zyh.service.VersionService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/version")
@Slf4j
public class VersionController {
    @Autowired
    private VersionService versionService;

    @PostMapping("/save")
    @ApiOperation("保存笔记版本")
    public Result saveVersion(@RequestParam Long articleId, @RequestBody Map<String, Object> data) {
        String yDocState = (String) data.get("yDocState");
        String title = (String) data.get("title");  // 新增标题的获取
        versionService.saveVersion(articleId, yDocState, title); // 同时传入标题
        return Result.success("版本保存成功");
    }


    @GetMapping("/{articleId}/{versionTimestamp}")
    @ApiOperation("获取笔记的特定版本")
    public Result<Map<String, Object>> getVersion(@PathVariable Long articleId, @PathVariable Long versionTimestamp) {
        String yDocState = versionService.getVersion(articleId, versionTimestamp);
        if (yDocState == null) {
            return Result.success("版本不存在");
        }
        Map<String, Object> result = new HashMap<>();
        result.put("yDocState", yDocState);
        return Result.success(result);
    }


    @GetMapping("/{articleId}/all")
    @ApiOperation("获取笔记的所有版本列表")
    public Result<List<VersionDTO>> getAllVersions(@PathVariable Long articleId) {
        List<VersionDTO> versions = versionService.getAllVersions(articleId);
        return Result.success(versions);
    }

}
